﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SqlSugar;
using System.Configuration;
using System.IO;

namespace SqlSugar
{
    public class SqlSugarDb
    {
        private static readonly string _connectString
            = ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
        public static SqlSugarClient GetInstance()
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = _connectString, DbType = DbType.MySql, IsAutoCloseConnection = true });
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                DateTime dateTime = DateTime.Now;
                try
                {
                    string exsql = sql;
                    foreach (SugarParameter sp in pars)
                    {
                        if (sp != null && sp.ParameterName != null)
                        {
                            string v = "''";
                            if (sp.Value != null)
                            {
                                v = "''";
                                try
                                {
                                    v = "'" + sp.Value.ToString() + "'";
                                }
                                catch (Exception ex)
                                {
                                    v = "'" + ex.Message.ToString() + "'";
                                }
                            }
                            else
                            {
                                v = "null";
                            }
                            exsql = exsql.Replace(sp.ParameterName, v);
                        }
                    }

                    string strPath = HttpContext.Current.Server.MapPath("~/") + "\\mylog";
                    if (Directory.Exists(strPath) == false)  //判断文件夹是否存在 如果 strPath 引用现有目录，则为 true；否则为 false。
                    {
                        Directory.CreateDirectory(strPath);  //创建文件夹
                    }

                    strPath = strPath + "\\" + dateTime.ToString("yyyyMM")+"\\sql";
                    if (Directory.Exists(strPath) == false)
                    {
                        Directory.CreateDirectory(strPath);
                    }

                    strPath = strPath + "\\" + dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString() + ".txt";

                    StreamWriter FileWriter = new StreamWriter(strPath, true);  //创建日志文件
                    FileWriter.Write(
                        "---------" + dateTime.ToString("yyyy-MM-dd HH:mm:ss") + "----------------------------------------------------------------------" + System.Environment.NewLine + sql + System.Environment.NewLine + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)) + System.Environment.NewLine + System.Environment.NewLine + exsql + System.Environment.NewLine);
                    FileWriter.Close();

                }
                catch(Exception ex)
                {

                }
            };
            return db;
        }
    }
}